﻿using DalatCooking.Core;
using DalatCooking.Core.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Product_Add_EditPicture :System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        CourseImage s = CourseImageManager.Single(ImageID);
        if (s != null)
        {
            txtServiceName.Text = CourseManager.Single(s.CourseID).Name;
            txtServiceID.Text = s.CourseID.ToString();
            txtPicture.Text = s.ImageUrl;
            imgthumb.Src = s.ImageUrl;
            cbPublish.Checked = s.IsPublished;
            txtAlt.Text = s.Alt;
            txtTitle.Text = s.Title;
        }
        txtImageID.Text = ImageID.ToString();
        base.OnInit(e);
    }

    public int ImageID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ImageID"]))
            {
                return ConvertObject.ToInt(Request.QueryString["ImageID"]);
            }
            return -1;
        }
    }

    void Add()
    {
        if (!string.IsNullOrEmpty(txtServiceID.Text) && !string.IsNullOrEmpty(txtPicture.Text))
        {
            CourseImage s = new CourseImage();
            s.ImageID = -1;
            s.CourseID = ConvertObject.ToInt(txtServiceID.Text);
            s.ImageUrl = txtPicture.Text;
            s.Alt = txtAlt.Text;
            s.Title = txtTitle.Text;
            s.IsPublished = cbPublish.Checked;
            bool rs = CourseImageManager.Insert(s);
            if (rs )
                Response.Redirect("Picture.aspx");
            Response.Write(rs);
        }
        else Response.Write(txtServiceID.Text);
    }
    void Update()
    {
        if (!string.IsNullOrEmpty(txtServiceID.Text) && !string.IsNullOrEmpty(txtPicture.Text))
        {
            CourseImage s = new CourseImage();
            s.ImageID = ImageID;
            s.CourseID = ConvertObject.ToInt(txtServiceID.Text);
            s.ImageUrl = txtPicture.Text;
            s.Alt = txtAlt.Text;
            s.Title = txtTitle.Text;
            s.IsPublished = cbPublish.Checked;
            bool rs = CourseImageManager.Update(s);
            if (rs)
                Response.Redirect("Picture.aspx");
            Response.Write(rs);
        }
        else Response.Write(txtServiceID.Text);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ImageID != -1)
        {
            Update();
        }
        else
        {
            Add();
        }
    }
}